library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.5     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   2.0.1     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(here)   # for specifying directory location
## here() starts at /Users/soltoffbc/Projects/css-skills-workshop/workshop-materials/geospatial-viz

Import 311 service requests

chi_311 <- read_csv(file = here("data", "chicago-311.csv"))
## Rows: 193299 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): sr_number, sr_short_code
## dbl  (4): community_area, ward, latitude, longitude
## dttm (1): created_date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
chi_311
## # A tibble: 193,299 × 7
##    sr_number     sr_short_code created_date        community_area  ward latitude
##    <chr>         <chr>         <dttm>                       <dbl> <dbl>    <dbl>
##  1 SR19-01209373 SGQ           2019-03-23 17:13:05             58    12     41.8
##  2 SR19-01129184 SGQ           2019-03-09 01:37:26             40    20     41.8
##  3 SR19-01130159 SGQ           2019-03-09 14:51:46             40    20     41.8
##  4 SR19-01142266 SGQ           2019-03-12 12:17:31             67    17     41.8
##  5 SR19-01142389 SGQ           2019-03-12 12:46:05             59    12     41.8
##  6 SR19-01142452 SGQ           2019-03-12 12:55:17             59    12     41.8
##  7 SR19-01137036 SGQ           2019-03-11 14:35:45              2    40     42.0
##  8 SR19-01142278 SGQ           2019-03-12 12:19:09             59    12     41.8
##  9 SR19-01142678 SGQ           2019-03-12 13:35:02             59    12     41.8
## 10 SR19-01142691 SGQ           2019-03-12 13:38:39             64    13     41.8
## # … with 193,289 more rows, and 1 more variable: longitude <dbl>

Obtain map tiles using ggmap for the city of Chicago.

# store bounding box coordinates
chi_bb <- c(
  left = -87.936287,
  bottom = 41.679835,
  right = -87.447052,
  top = 42.000835
)

# retrieve bounding box
chicago <- get_stamenmap(
  bbox = chi_bb,
  zoom = 11
)
## Source : http://tile.stamen.com/terrain/11/523/760.png
## Source : http://tile.stamen.com/terrain/11/524/760.png
## Source : http://tile.stamen.com/terrain/11/525/760.png
## Source : http://tile.stamen.com/terrain/11/526/760.png
## Source : http://tile.stamen.com/terrain/11/523/761.png
## Source : http://tile.stamen.com/terrain/11/524/761.png
## Source : http://tile.stamen.com/terrain/11/525/761.png
## Source : http://tile.stamen.com/terrain/11/526/761.png
## Source : http://tile.stamen.com/terrain/11/523/762.png
## Source : http://tile.stamen.com/terrain/11/524/762.png
## Source : http://tile.stamen.com/terrain/11/525/762.png
## Source : http://tile.stamen.com/terrain/11/526/762.png
# plot the raster map
ggmap(chicago)

Generate a scatterplot of complaints about potholes in streets.

# initialize map
ggmap(chicago) +
  # add layer with scatterplot
  # use alpha to show density of points
  geom_point(
    data = filter(chi_311, sr_short_code == "PHF"),
    mapping = aes(
      x = longitude,
      y = latitude
    ),
    size = .25,
    alpha = .05
  )
## Warning: Removed 8863 rows containing missing values (geom_point).

Generate a heatmap of complaints about potholes in streets. Do you see any unusual patterns or clusterings?

# initialize the map
ggmap(chicago) +
  # add the heatmap
  stat_density_2d(
    data = filter(chi_311, sr_short_code == "PHF"),
    mapping = aes(
      x = longitude,
      y = latitude,
      fill = stat(level)
    ),
    alpha = .1,
    bins = 50,
    geom = "polygon"
  )
## Warning: Removed 8863 rows containing non-finite values (stat_density2d).

Obtain map tiles for Hyde Park using the toner map tiles

# store bounding box coordinates
hp_bb <- c(
  left = -87.608221,
  bottom = 41.783249,
  right = -87.577643,
  top = 41.803038
)

# retrieve bounding box
hyde_park <- get_stamenmap(
  bbox = hp_bb,
  zoom = 15,
  maptype = "toner"
)
## Source : http://tile.stamen.com/toner/15/8409/12188.png
## Source : http://tile.stamen.com/toner/15/8410/12188.png
## Source : http://tile.stamen.com/toner/15/8411/12188.png
## Source : http://tile.stamen.com/toner/15/8412/12188.png
## Source : http://tile.stamen.com/toner/15/8409/12189.png
## Source : http://tile.stamen.com/toner/15/8410/12189.png
## Source : http://tile.stamen.com/toner/15/8411/12189.png
## Source : http://tile.stamen.com/toner/15/8412/12189.png
## Source : http://tile.stamen.com/toner/15/8409/12190.png
## Source : http://tile.stamen.com/toner/15/8410/12190.png
## Source : http://tile.stamen.com/toner/15/8411/12190.png
## Source : http://tile.stamen.com/toner/15/8412/12190.png
# plot the raster map
ggmap(hyde_park)

Generate a scatterplot of requests to pick up dead animals in Hyde Park

# initialize the map
ggmap(hyde_park) +
  # add a scatterplot layer
  geom_point(
    data = filter(chi_311, sr_short_code == "SGQ"),
    mapping = aes(
      x = longitude,
      y = latitude
    )
  )
## Warning: Removed 34287 rows containing missing values (geom_point).

Session Info

sessioninfo::session_info()
## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 4.1.0 (2021-05-18)
##  os       macOS Big Sur 10.16         
##  system   x86_64, darwin17.0          
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  ctype    en_US.UTF-8                 
##  tz       America/Chicago             
##  date     2021-10-18                  
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package     * version date       lib source        
##  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.0)
##  backports     1.2.1   2020-12-09 [1] CRAN (R 4.1.0)
##  bit           4.0.4   2020-08-04 [1] CRAN (R 4.1.0)
##  bit64         4.0.5   2020-08-30 [1] CRAN (R 4.1.0)
##  bitops        1.0-7   2021-04-24 [1] CRAN (R 4.1.0)
##  broom         0.7.9   2021-07-27 [1] CRAN (R 4.1.0)
##  bslib         0.2.5.1 2021-05-18 [1] CRAN (R 4.1.0)
##  cellranger    1.1.0   2016-07-27 [1] CRAN (R 4.1.0)
##  cli           3.0.1   2021-07-17 [1] CRAN (R 4.1.0)
##  colorspace    2.0-2   2021-06-24 [1] CRAN (R 4.1.0)
##  crayon        1.4.1   2021-02-08 [1] CRAN (R 4.1.0)
##  curl          4.3.2   2021-06-23 [1] CRAN (R 4.1.0)
##  DBI           1.1.1   2021-01-15 [1] CRAN (R 4.1.0)
##  dbplyr        2.1.1   2021-04-06 [1] CRAN (R 4.1.0)
##  digest        0.6.27  2020-10-24 [1] CRAN (R 4.1.0)
##  dplyr       * 1.0.7   2021-06-18 [1] CRAN (R 4.1.0)
##  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.0)
##  evaluate      0.14    2019-05-28 [1] CRAN (R 4.1.0)
##  fansi         0.5.0   2021-05-25 [1] CRAN (R 4.1.0)
##  farver        2.1.0   2021-02-28 [1] CRAN (R 4.1.0)
##  forcats     * 0.5.1   2021-01-27 [1] CRAN (R 4.1.0)
##  fs            1.5.0   2020-07-31 [1] CRAN (R 4.1.0)
##  generics      0.1.0   2020-10-31 [1] CRAN (R 4.1.0)
##  ggmap       * 3.0.0   2019-02-05 [1] CRAN (R 4.1.0)
##  ggplot2     * 3.3.5   2021-06-25 [1] CRAN (R 4.1.0)
##  glue          1.4.2   2020-08-27 [1] CRAN (R 4.1.0)
##  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.1.0)
##  haven         2.4.3   2021-08-04 [1] CRAN (R 4.1.0)
##  here        * 1.0.1   2020-12-13 [1] CRAN (R 4.1.0)
##  highr         0.9     2021-04-16 [1] CRAN (R 4.1.0)
##  hms           1.1.0   2021-05-17 [1] CRAN (R 4.1.0)
##  htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.1.0)
##  httr          1.4.2   2020-07-20 [1] CRAN (R 4.1.0)
##  isoband       0.2.5   2021-07-13 [1] CRAN (R 4.1.0)
##  jpeg          0.1-9   2021-07-24 [1] CRAN (R 4.1.0)
##  jquerylib     0.1.4   2021-04-26 [1] CRAN (R 4.1.0)
##  jsonlite      1.7.2   2020-12-09 [1] CRAN (R 4.1.0)
##  knitr         1.33    2021-04-24 [1] CRAN (R 4.1.0)
##  labeling      0.4.2   2020-10-20 [1] CRAN (R 4.1.0)
##  lattice       0.20-44 2021-05-02 [1] CRAN (R 4.1.0)
##  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.1.0)
##  lubridate     1.7.10  2021-02-26 [1] CRAN (R 4.1.0)
##  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.0)
##  MASS          7.3-54  2021-05-03 [1] CRAN (R 4.1.0)
##  modelr        0.1.8   2020-05-19 [1] CRAN (R 4.1.0)
##  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.1.0)
##  pillar        1.6.3   2021-09-26 [1] CRAN (R 4.1.0)
##  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.0)
##  plyr          1.8.6   2020-03-03 [1] CRAN (R 4.1.0)
##  png           0.1-7   2013-12-03 [1] CRAN (R 4.1.0)
##  purrr       * 0.3.4   2020-04-17 [1] CRAN (R 4.1.0)
##  R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.0)
##  Rcpp          1.0.7   2021-07-07 [1] CRAN (R 4.1.0)
##  readr       * 2.0.1   2021-08-10 [1] CRAN (R 4.1.0)
##  readxl        1.3.1   2019-03-13 [1] CRAN (R 4.1.0)
##  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.1.0)
##  RgoogleMaps   1.4.5.3 2020-02-12 [1] CRAN (R 4.1.0)
##  rjson         0.2.20  2018-06-08 [1] CRAN (R 4.1.0)
##  rlang         0.4.11  2021-04-30 [1] CRAN (R 4.1.0)
##  rmarkdown     2.10    2021-08-06 [1] CRAN (R 4.1.0)
##  rprojroot     2.0.2   2020-11-15 [1] CRAN (R 4.1.0)
##  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)
##  rvest         1.0.1   2021-07-26 [1] CRAN (R 4.1.0)
##  sass          0.4.0   2021-05-12 [1] CRAN (R 4.1.0)
##  scales        1.1.1   2020-05-11 [1] CRAN (R 4.1.0)
##  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.1.0)
##  sp            1.4-5   2021-01-10 [1] CRAN (R 4.1.0)
##  stringi       1.7.3   2021-07-16 [1] CRAN (R 4.1.0)
##  stringr     * 1.4.0   2019-02-10 [1] CRAN (R 4.1.0)
##  tibble      * 3.1.5   2021-09-30 [1] CRAN (R 4.1.0)
##  tidyr       * 1.1.3   2021-03-03 [1] CRAN (R 4.1.0)
##  tidyselect    1.1.1   2021-04-30 [1] CRAN (R 4.1.0)
##  tidyverse   * 1.3.1   2021-04-15 [1] CRAN (R 4.1.0)
##  tzdb          0.1.2   2021-07-20 [1] CRAN (R 4.1.0)
##  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.0)
##  vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.1.0)
##  vroom         1.5.4   2021-08-05 [1] CRAN (R 4.1.0)
##  withr         2.4.2   2021-04-18 [1] CRAN (R 4.1.0)
##  xfun          0.25    2021-08-06 [1] CRAN (R 4.1.0)
##  xml2          1.3.2   2020-04-23 [1] CRAN (R 4.1.0)
##  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.1.0)
## 
## [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library